package com.easytake.emotioncat.controller;

import com.easytake.emotioncat.dto.PageResult;
import com.easytake.emotioncat.dto.Response;
import com.easytake.emotioncat.entity.EmFeedbackEntity;
import com.easytake.emotioncat.entity.EmUserInfoEntity;
import com.easytake.emotioncat.fileio.FileUtil;
import com.easytake.emotioncat.fileio.minio.MinioConfig;
import com.easytake.emotioncat.param.EmFeedbackParam;
import com.easytake.emotioncat.service.IEmFeedbackService;
import com.easytake.emotioncat.service.IEmUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 反馈记录表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-11-02
 */
@Tag(name = "建议反馈接口")
@RestController
@RequestMapping("/emFeedback")
public class EmFeedbackController {

    @Autowired
    IEmFeedbackService emFeedbackService;

    @Autowired
    MinioConfig minioConfig;

    @Autowired
    FileUtil fileUtil;

    @Autowired
    IEmUserInfoService userInfoService;


    @Operation(summary = "保存建议")
    @PostMapping()
    //@RequireToken
    public Response<EmFeedbackEntity> save(@RequestHeader String wx_uid, @Validated EmFeedbackParam feedbackParam, MultipartFile[] files) {
        EmUserInfoEntity userInfoEntity = userInfoService.queryByUserId(wx_uid);
        if (Objects.isNull(userInfoEntity)) {
            throw new IllegalArgumentException("用户不存在");
        }
        EmFeedbackEntity entity = new EmFeedbackEntity();
        BeanUtils.copyProperties(feedbackParam, entity);
        if (Objects.nonNull(files) && minioConfig.getEnable()) {
            String path = fileUtil.upload(files);
            entity.setImages(path);
        }
        entity.setUserId(wx_uid);
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        emFeedbackService.save(entity);
        return Response.success(entity);
    }

    @Operation(summary = "分页查询建议(内部查看用不对外开放)")
    @GetMapping
    @Parameter(name = "pageNo", description = "页码")
    @Parameter(name = "pageSize", description = "每页显示数量")
    public Response<PageResult<EmFeedbackEntity>> getList(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        PageResult<EmFeedbackEntity> emPageResult = emFeedbackService.getListByPage( pageNo, pageSize);
        List<EmFeedbackEntity> entityList = emPageResult.getResults();
        if (!minioConfig.getEnable()) {
            return Response.success(emPageResult);
        }
        //补充图片完整路径
        for (EmFeedbackEntity entity : entityList) {
            String images = entity.getImages();
            if (StringUtils.isNotBlank(images)) {
                String fullPathImages = Arrays.stream(images.split(";")).map(i -> fileUtil.generateFileUrl(i)).collect(Collectors.joining(";"));
                entity.setImages(fullPathImages);
            }
        }
        return Response.success(emPageResult);
    }
}
